<template>
  <div class="app-container">
    <vxe-grid
      ref="gridRef"
      v-bind="gridOptions"
      @toolbar-button-click="toolbarButtonClickEvent"
      @form-submit="formSubmitEvent"
      @form-reset="formResetEvent"
    >
      <template #active="{ row }">
        <vxe-button
          mode="text"
          status="primary"
          content="修改"
          icon="vxe-icon-edit"
          @click="updateRow(row)"
        ></vxe-button>
        <vxe-button
          mode="text"
          status="primary"
          content="设计"
          icon="vxe-icon-edit"
          @click="designRow(row)"
        ></vxe-button>
        <vxe-button
          mode="text"
          status="primary"
          content="部署"
          icon="vxe-icon-upload"
          @click="deploy(row)"
        ></vxe-button>
        <vxe-button
          mode="text"
          status="primary"
          content="导出"
          icon="vxe-icon-download"
          @click="removeRow(row)"
        ></vxe-button>
        <vxe-button
          mode="text"
          status="primary"
          content="历史版本"
          icon="vxe-icon-download"
          @click="removeRow(row)"
        ></vxe-button>
      </template>
    </vxe-grid>

    <vxe-modal v-model="showPopup" v-bind="modalOptions" :loading="loading">
      <template #default>
        <vxe-form
          v-bind="formOptions"
          @submit="submitEvent"
          @reset="resetEvent"
        ></vxe-form>
      </template>
    </vxe-modal>
  </div>
</template>

<script>
import {
  getEquipmentLevelByPage,
  addEquipmentLevel,
} from "@/api/baseData/equipmentLevel";
import { VxeUI } from "vxe-pc-ui";

import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const findPageList = async (page) => {
  await getMaterialByPage(page);
  // return new Promise(resolve => {
  //   setTimeout(() => {
  //     resolve({
  //       result: list.slice((currentPage - 1) * pageSize, currentPage * pageSize),
  //       page: {
  //         total: list.length
  //       }
  //     })
  //   }, 100)
  // })
};
export default {
  data() {
    const gridOptions = {
      sortConfig: {
        remote: true,
      },
      loading: false,
      stripe: true,
      showOverflow: true,
      showFooter: true,
      height: 800,
      columnConfig: {
        resizable: true,
        drag: true,
      },
      columnDragConfig: {
        trigger: "cell",
        showIcon: false,
        showGuidesStatus: true,
      },
      rowConfig: {
        isHover: true,
      },
      resizableConfig: {
        isDblclickAutoWidth: true,
      },
      pagerConfig: {
        total: 0,
        currentPage: 1,
        pageSize: 10,
      },
      toolbarConfig: {
        refresh: true,
        import: true,
        export: true,
        print: true,
        zoom: true,
        custom: true,
        buttons: [
          { name: "新增", code: "myAdd", status: "primary" },
          { name: "删除", code: "myDel", status: "error" },
          { name: "保存", code: "mySave", status: "success" },
        ],
      },
      formConfig: {
        data: {
          name: "",
          role: "",
          sex: "",
          num: "",
          address: "",
        },
        items: [
          {
            field: "name",
            title: "名称",
            span: 8,

            itemRender: { name: "VxeInput", props: { clearable: true } },
          },

          {
            field: "createTime",
            title: "时间",
            span: 8,

            itemRender: {
              name: "VxeDatePicker",
              props: {
                multiple: true,
              },
            },
          },

          {
            collapseNode: true,
            align: "right",
            span: 8,
            itemRender: {
              name: "VxeButtonGroup",
              options: [
                { type: "submit", content: "搜索", status: "primary" },
                { type: "reset", content: "重置" },
              ],
            },
          },
          {
            field: "createTime",
            title: "时间",
            span: 8,
            folding: true,
            itemRender: {
              name: "VxeDatePicker",
              props: {
                multiple: true,
              },
            },
          },
        ],
      },
      columns: [
        { field: "seq", type: "seq", width: 70 },
        { field: "id", title: "id", sortable: true, width: 180 },
        { field: "code", title: "编码", sortable: true, width: 180 },
        {
          field: "name",
          title: "名称",
          sortable: true,
          width: 180,
        },
        { field: "createTime", title: "创建时间", width: 180 },
        {
          field: "active",
          title: "操作",
          fixed: "right",
          width: 400,
          slots: { default: "active" },
        },
      ],
      scrollX: {
        gt: 0,
        enabled: true,
      },
      scrollY: {
        gt: 0,
        enabled: true,
      },
      proxyConfig: {
        // props: {
        //   result: 'result', // 配置响应结果列表字段
        //   total: 'page.total' // 配置响应结果总页数字段
        // },

        sort: true,
        form: true,
        ajax: {
          query: async ({ page, sorts, form }) => {
            console.log(page, sorts, form, "----");
            // 默认接收 Promise<{ result: [], page: { total: 100 } }>
            let result = await getEquipmentLevelByPage({
              ...page,
            });
            return {
              result: result.data.records,
              page: {
                total: result.data.total,
              },
            };
          },
        },
      },
    };
    const showPopup = false;
    const modalOptions = {
      title: "在弹窗中使用表单",
      width: 800,

      escClosable: true,
      resize: true,
      showMaximize: true,
      //禁止拖动
      draggable: true,
    };
    const sexItemRender = {
      name: "VxeSelect",
      options: [
        { label: "女", value: "Women" },
        { label: "男", value: "Man" },
      ],
    };
    const formOptions = {
      data: {
        name: "",
        role: "",
        sex: "",
        age: "",
        address: "",
      },
      rules: {
        name: [{ required: true, message: "名称必填" }],
        // role: [{ required: true, message: "角色必填" }],
        // sex: [{ required: true, message: "性别必选" }],
        // age: [
        //   { required: true, message: "年龄必填" },
        //   { type: "number", min: 18, max: 46, message: "年龄范围18~46" },
        // ],
      },
      items: [
        {
          field: "code",
          title: "编码",
          span: 24,
          itemRender: { name: "VxeInput" },
        },
        {
          field: "name",
          title: "名称",
          span: 24,
          itemRender: { name: "VxeInput" },
        },
        // {
        //   field: "role",
        //   title: "角色",
        //   span: 24,
        //   itemRender: { name: "VxeInput" },
        // },
        // { field: "sex", title: "性别", span: 12, itemRender: sexItemRender },
        // {
        //   field: "age",
        //   title: "年龄",
        //   span: 12,
        //   itemRender: { name: "VxeNumberInput", props: { type: "integer" } },
        // },
        // {
        //   field: "address",
        //   title: "地址",
        //   span: 24,
        //   itemRender: { name: "VxeTextarea" },
        // },
        {
          align: "center",
          span: 24,
          itemRender: {
            name: "VxeButtonGroup",
            options: [
              { type: "submit", content: "提交", status: "primary" },
              { type: "reset", content: "重置" },
            ],
          },
        },
      ],
    };
    return {
      gridOptions,
      showPopup,
      modalOptions,
      sexItemRender,
      formOptions,
      loading: false,
    };
  },
  methods: {
    toolbarButtonClickEvent(params) {
      if (params.code == "myAdd") {
        this.showPopup = true;
      }
      console.log(params.code);
    },
    formSubmitEvent() {
      console.log("form submit");
    },
    formResetEvent() {
      console.log("form reset");
    },
    //表单的提交
    async submitEvent() {
      this.loading = true;
      await addEquipmentLevel(this.formOptions.data);
      console.log("保存成功", JSON.stringify(this.formOptions.data));
      const $grid = this.$refs.gridRef.commitProxy("reload");
      VxeUI.modal.message({ content: "保存成功", status: "success" });
      this.loading = false;
      this.showPopup = false;
    },
    //表单的重置
    resetEvent() {
      console.log("重置事件");
    },
    updateRow(row) {
      this.formOptions.data = row;
      this.showPopup = true;
      console.log(row.id, "---row---");
    },
    async deploy(row) {
      this.formOptions.data = row;
      console.log(row.id, "---row---");
      await camundaModelDeploymentById(row);
    },
    designRow(row) {
      this.$router.push({
        path: "/flowable/bpmtest",
        query: {
          id: row.id,
        },
      });

      console.log(row.id, "---row---");
    },
  },
};
</script>
